package com.heima.admin.job;


import com.heima.admin.service.WemediaNewsAutoScanService;
import com.heima.feigns.WemediaFeign;
import com.heima.model.common.dtos.ResponseResult;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class WeMediaNewsAutoScanJob {

    @Autowired
    WemediaFeign wemediaFeign;

    @Autowired
    WemediaNewsAutoScanService wemediaNewsAutoScanService;


    @XxlJob("wemediaAutoScanJob")
    public ReturnT WeMediaNewsAutoScanJob(String params){
        log.info("定时发布任务被触发   start=====");
        //远程调用wemedia查询待发布文章id列表
        ResponseResult<List<Integer>> releaseResult = wemediaFeign.findRelease();
        if (releaseResult.getCode().intValue() != 0) {
            log.error("远程调用wemedia服务出现异常，原因：{}",releaseResult.getErrorMessage());
            return ReturnT.FAIL;
        }
        List<Integer> newsIdList = releaseResult.getData();
        if (CollectionUtils.isNotEmpty(newsIdList)) {
            for (Integer newsId : newsIdList) {
            //调用自动审核发布方法 发布文章
                wemediaNewsAutoScanService.autoScanByMediaNewsId(newsId);
            }
        }
        log.info("定时发布任务被触发   end=====");
        return ReturnT.SUCCESS;
    }
}
